<?php
/**
 * Created by PhpStorm.
 * User: 狗剩先生
 * Date: 2018/12/23
 * Time: 0:01
 */

namespace app\admin\controller;

use think\Controller;

class User extends Controller
{
    #   默认显示
    public function index()
    {

            $res = model('user')->getUser();
            $length = model('user')->where('id', '>', 0)->select();
            return $this->fetch('', [
                'dataLength' => sizeof($length),
                'userInfo' => $res
            ]);



    }
    #   添加
    public function add()
    {
        if ($this->request->isPost()) {
            $post = input();
            $data = model('user')->data($post)->save();
            return json($data);
        }else {
            return $this->fetch();
        }

    }
    #   修改
    public function edit($id)
    {
        # 判断用户有没有对内容修改
        if($this->request->isPost()){
            $post = input();
            $data = db('user')->where('id',$id)->data($post)->update();
            return json($data);
        } else {
            $data = db('user')->where('id',$id)->find();
            return $this->fetch('', [
                'data'  => $data
            ]);
        }
    }

    public function member ()
    {


    }
    #   嵌套搜索
    public function search ()
    {
            $sdata = input('get.');
            dump($sdata);
            $values = [];
            if (!empty($sdata['username'])) {
                $values['username'] = ['like', '%'.$sdata['username'].'%'];
            }
            print_r($values);
            $res = model('user')->getSearchResult($values);
            $length = model('user')->where('id', '>', 0)->select();
            return $this->fetch('', [
                'dataLength' => sizeof($length),
                'userInfo' => $res
            ]);
    }

    #   删除
    public function delete()
    {
        $post = input('');
        $res = db('user')->delete($post);
        if($res){
            return json([
                'msg'=>'删除成功',
                'status'=>1
            ]
           );
        }else {
            return json([
                'msg'=>'删除失败,找不到该条数据',
                'status'=>0
            ]);
        }
    }
}